package com.wugui.datatx.core.handler.impl;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;

import com.wugui.datatx.core.biz.model.ReturnT;
import com.wugui.datatx.core.biz.model.TriggerParam;
import com.wugui.datatx.core.handler.IJobHandler;
import com.wugui.datatx.core.log.JobLogger;
import com.wugui.datatx.core.util.DateUtil;
import com.wugui.datatx.core.util.TimeStackUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
@ConditionalOnClass(IJobHandler.class)
public abstract class MyJobHandler extends IJobHandler {
    @Override
    public ReturnT<String> execute(TriggerParam param) throws Exception {
        log("【{}】任务开始: {} 间隔：{}s", getClass().getSimpleName(), DateUtil.formatDateTime(param.getStartTime()), param.getIncStepSecond());
        log("【{}】任务参数: {}", getClass().getSimpleName(), param.getExecutorParams());
        log("【{}】替换参数: {}", getClass().getSimpleName(), param.getReplaceParam());
        log("【{}】Jvm参数: {}", getClass().getSimpleName(), param.getJvmParam());
        TimeStackUtils.begin();
        try {
            return myExecute(param);
        } catch (Exception e) {
            log("【{}】{} {}遇到异常情况！ {}", getClass().getSimpleName(), param.getExecutorParams(), DateUtil.formatDateTime(param
                .getStartTime()),
                e.getMessage());
            JobLogger.log(e);
            return FAIL;
        } finally {
            log("【{}】任务完成!!! {} {} 耗时：{}ms", getClass().getSimpleName(), param.getExecutorParams(), DateUtil.formatDateTime(param.getStartTime()),
                TimeStackUtils.end());
        }
    }

    protected abstract ReturnT<String> myExecute(TriggerParam params) throws Exception;

    public static void log(String appendLogPattern, Object... appendLogArguments) {
        JobLogger.log(appendLogPattern, appendLogArguments);
    }

}
